//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    imgUrls: [
      
    ],
    defaultImgUrl:'../../images/banner.jpg',
    num:1,
    //图片列表
    goodsinfo:[

    ],
    classify:[
      
    ],
    //下拉显示的信息
    classifyMore:[

    ],
    more_pic:"../../images/s_pic5.jpg",
    indicatorDots:false,
    indicatorColor:"rgba(0,0,0,.3)",
    autoplay:true,
    height:"",
    activeColor:"#FDD001",
    swiperCurrent:"",
    oheight: "",//设置分类图片高
    tag:false,//更多分类是否展开标识
    more_tag:true,//更多分类是否显示
    currentPage:1,
    homeHeight:''//列表图片高度
  },
  imageLoad(e) {
    // console.log(wx.getSystemInfoSync().windowWidth)
    //图片真实高度
    let imgwidth = e.detail.width
    let imgheight = e.detail.height
    //图片展示宽度
    let imgwidth2 = wx.getSystemInfoSync().windowWidth
    let ratio = imgwidth / imgheight
    let height2 = imgwidth2 / ratio
    this.setData({
      height: height2
    })
  },
  get_index(e){
    this.setData({
      swiperCurrent: e.detail.current
    })
    console.log(this.data.swiperCurrent)
  },
  getUserInfo(e){
    console.log(e);
  },
  onLoad() {
    // wx.showShareMenu({
    //   withShareTicket: true,
    //   complete:function(res) {
    //     console.log(res)
    //   }
    // })


    let _this = this
    let Owidth = (wx.getSystemInfoSync().windowWidth*0.9)/5
    console.log(Owidth)
    this.setData({
      oheight:Owidth
    })
    //获取banner图
    wx.request({
      url: app.globalData.url + '/homepage/banners',
      method: 'post',
      header: {
        "content-type": 'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      success(res) {
        console.log("MALL_USER_COOKIE", app.globalData.MALL_USER_COOKIE)
        console.log("MALL_WECHAT_COOKIE",app.globalData.MALL_WECHAT_COOKIE)
        console.log('获取banner图', res)
        _this.setData({
          imgUrls: res.data.banners,
          num: res.data.banners.length
        })
      }
    })
    //获取更多分类
    wx.request({
      url: app.globalData.url +'/homepage/categorys',
      method:"post",
      header:{
        "content-type":"application/x-www-form-urlencodled",
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      success(res) {
        console.log("更多分类",res)

        _this.setData({
          classify:res.data.categorys.slice(0,4),
          classifyMore: res.data.categorys.slice(3, res.data.categorys.length-1),
          more_tag: res.data.categorys.length>=4 ? true : false
        })
      }
    }),
    //商品列表
    wx.request({
      url: app.globalData.url +'/homepage/products',
      method:'post',
      header:{
        'content-type':'application/x-www-form-urlencoded',
        "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
        "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
      },
      data:{
        currentPageForApp: _this.data.currentPage,
        showCount:10
      },
      success(res) {
        console.log("首页商品列表",res)
        let arr1 = res.data.products
        let arr2 = []
        arr1.forEach(function(value,index){
          JSON.parse(value.images).forEach(function(value2,index2){
            if (value2.isDefalut == 0){
              arr2.push(value2)
              value.imageUrl = arr2[index].imageUrl
            }
          })
        })
        _this.setData({
          goodsinfo: arr1
        })
        // setTimeout(function() {
        //   arr1.forEach(function(value,index) {
        //     value.imageUrl = arr2[index].imageUrl
        //   })
        //   _this.setData({
        //     goodsinfo:arr1
        //   })
        // },1000)
      }
    })
  },
  onShareAppMessage(res){
    return {
      title:"奇幻仓库",
      path:"/pages/user/user?meg=haomowei"
    }
  },
  onPullDownRefresh(res){
    setTimeout(function() {
      wx.stopPullDownRefresh()
    },2000)
  },
  //下拉加载
  onReachBottom() {
    const _this = this
    let flag = 1
    if(flag == 1){
      flag = 0
      _this.setData({
        currentPage: _this.data.currentPage + 1
      })
      wx.request({
        url: app.globalData.url + '/homepage/products',
        method: 'post',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          "MALL_USER_COOKIE": app.globalData.MALL_USER_COOKIE,
          "MALL_WECHAT_COOKIE": app.globalData.MALL_WECHAT_COOKIE
        },
        data: {
          currentPageForApp: _this.data.currentPage,
          showCount: 10
        },
        success(res) {
          let arr1 = []
          arr1 = _this.data.goodsinfo
          let arr2 = res.data.products
          let arr3 = []
          arr2.forEach(function(value,index) {
            JSON.parse(value.images).forEach(function(value2,index2) {
              if(value2.isDefalut == 0) {
                arr3.push(value2)
                value.imageUrl = arr3[index].imageUrl
              }
            })
            arr1.push(value)
          })
          console.log('首页上滑加载数据',arr2)
          _this.setData({
            goodsinfo:arr1
          })
        }
      })
    }
  },
  goDetail(event) {
    wx.navigateTo({
      url: '/pages/detail/detail?guid=' + event.currentTarget.dataset.guid,
      success(res) {
        // console.log('/pages/detail?id=' + event.currentTarget.dataset.index)
      }
    })
  },
  changMore(){
    this.setData({
      tag:!this.data.tag
    })
    console.log("tag",this.data.tag)
  },
  homeList(res) {
    //图片真实高度
    let Oheight = res.detail.height
    let Owidth = res.detail.width
    //屏幕宽度
    let winWidth = wx.getSystemInfoSync().windowWidth

    let myHeight = winWidth*Oheight/Owidth
    this.setData({
      homeHeight:myHeight
    })
  }
})
